package com.cloud.code.真题;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

/**
 * 小朋友排队
 */
public class Demo11 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int xiaoming = scanner.nextInt();
            int n = scanner.nextInt();
            List<Integer> list = new ArrayList<>();

            for (int i = 0; i < n; i++) {
                list.add(scanner.nextInt());
            }

            list.sort(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    int o1abs = Math.abs(o1 - xiaoming);
                    int o2abs = Math.abs(o2 - xiaoming);
                    if (o1abs != o2abs) {
                        return o1abs - o2abs;
                    } else {
                        return o1 - o2;
                    }
                }
            });

            System.out.println(list);

        }
    }
}
